home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1995 May / PC Answers CD-ROM 7 (Future Publishing) (May 1995).iso / vbits / code / appleman / evtprog2.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-10-06  |  6.6 KB  |  221 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "Designing Event Driven Code"
  4.    ClientHeight    =   3900
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1485
  7.    ClientWidth     =   5580
  8.    Height          =   4305
  9.    Left            =   1035
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   3900
  12.    ScaleWidth      =   5580
  13.    Top             =   1140
  14.    Width           =   5700
  15.    Begin CommandButton Command7 
  16.       Caption         =   "Cancel Timer"
  17.       Height          =   435
  18.       Left            =   3060
  19.       TabIndex        =   7
  20.       Top             =   3360
  21.       Width           =   1755
  22.    End
  23.    Begin Timer Timer1 
  24.       Enabled         =   0   'False
  25.       Interval        =   1
  26.       Left            =   180
  27.       Top             =   2940
  28.    End
  29.    Begin CommandButton Command6 
  30.       Caption         =   "With Timer"
  31.       Height          =   435
  32.       Left            =   3060
  33.       TabIndex        =   6
  34.       Top             =   2880
  35.       Width           =   1755
  36.    End
  37.    Begin CommandButton Command5 
  38.       Caption         =   "With DoEvents III"
  39.       Height          =   495
  40.       Left            =   3060
  41.       TabIndex        =   5
  42.       Top             =   2340
  43.       Width           =   1755
  44.    End
  45.    Begin CommandButton Command4 
  46.       Caption         =   "With DoEvents II"
  47.       Height          =   495
  48.       Left            =   3060
  49.       TabIndex        =   4
  50.       Top             =   1800
  51.       Width           =   1755
  52.    End
  53.    Begin CommandButton Command3 
  54.       Caption         =   "With DoEvents"
  55.       Height          =   495
  56.       Left            =   3060
  57.       TabIndex        =   3
  58.       Top             =   1260
  59.       Width           =   1755
  60.    End
  61.    Begin CommandButton Command2 
  62.       Caption         =   "Escape Check"
  63.       Height          =   495
  64.       Left            =   3060
  65.       TabIndex        =   2
  66.       Top             =   720
  67.       Width           =   1695
  68.    End
  69.    Begin CommandButton Command1 
  70.       Caption         =   "No DoEvents"
  71.       Height          =   495
  72.       Left            =   3060
  73.       TabIndex        =   1
  74.       Top             =   180
  75.       Width           =   1695
  76.    End
  77.    Begin Label Label1 
  78.       BackColor       =   &H00FFFFFF&
  79.       Caption         =   "Label1"
  80.       FontBold        =   -1  'True
  81.       FontItalic      =   0   'False
  82.       FontName        =   "MS Sans Serif"
  83.       FontSize        =   24
  84.       FontStrikethru  =   0   'False
  85.       FontUnderline   =   0   'False
  86.       Height          =   675
  87.       Left            =   300
  88.       TabIndex        =   0
  89.       Top             =   420
  90.       Width           =   2295
  91.    End
  92. Option Explicit
  93. ' With no events allowed, not only are further clicks
  94. ' not acted upon, but they are queued up for later - leaving
  95. ' to results confusing to the user.
  96. Sub Command1_Click ()
  97.     Dim x&
  98.     For x& = 1 To LOOPCOUNT
  99.         If x& = 500 Then ToggleColor
  100.         label1.Caption = Str$(x&)
  101.         label1.Refresh
  102.     Next x&
  103. End Sub
  104. ' A classic DOS approach is to check for a key such as
  105. ' the escape key.  But this still allows queued events
  106. ' to pile up.
  107. Sub Command2_Click ()
  108.     Dim x&
  109.     Dim EscapeKey%
  110.     ' Clear the current state
  111.     EscapeKey% = GetAsyncKeyState(VK_ESCAPE)
  112.     For x& = 1 To LOOPCOUNT
  113.         If x& = 500 Then ToggleColor
  114.         label1.Caption = Str$(x&)
  115.         label1.Refresh
  116.         EscapeKey% = GetAsyncKeyState(VK_ESCAPE)
  117.         If EscapeKey% And 1 Then Exit Sub
  118.     Next x&
  119. End Sub
  120. ' This time we place a DoEvents to allow events to be
  121. ' processed - but note the reentrancy problem!
  122. Sub Command3_Click ()
  123.     Dim x&
  124.     For x& = 1 To LOOPCOUNT
  125.         label1.Caption = Str$(x&)
  126.         If x& = 500 Then ToggleColor
  127.         ' Note - we don't need the refresh any more
  128.         DoEvents
  129.     Next x&
  130. End Sub
  131. ' We can prevent reentrancy problems by disabling the form
  132. Sub Command4_Click ()
  133.     Dim x&
  134.     ' The easy way is to disable the entire form
  135.     Form1.Enabled = False
  136.     ' Alternatively, you can disable each control
  137.     ' individually (it would look better)
  138.     For x& = 1 To LOOPCOUNT
  139.         label1.Caption = Str$(x&)
  140.         If x& = LOOPCOUNT Then ToggleColor
  141.         ' Note - we don't need the refresh any more
  142.         DoEvents
  143.     Next x&
  144.     ' And be sure to reenable the form when done
  145.     Form1.Enabled = True
  146. End Sub
  147. ' The disabling might look better if we do it one control
  148. ' at a time
  149. Sub Command5_Click ()
  150.     Dim x&
  151.     Dim ctlnum%
  152.     ' Alternatively, you can disable each control
  153.     ' individually (it would look better)
  154.     For ctlnum% = 0 To Controls.Count - 1
  155.         If TypeOf Controls(ctlnum%) Is CommandButton Then
  156.             Controls(ctlnum%).Enabled = False
  157.         End If
  158.     Next ctlnum%
  159.     For x& = 1 To LOOPCOUNT
  160.         label1.Caption = Str$(x&)
  161.         If x& = LOOPCOUNT Then ToggleColor
  162.         ' Note - we don't need the refresh any more
  163.         DoEvents
  164.     Next x&
  165.     ' And be sure to reenable the controls when done
  166.     For ctlnum% = 0 To Controls.Count - 1
  167.         If TypeOf Controls(ctlnum%) Is CommandButton Then
  168.             Controls(ctlnum%).Enabled = True
  169.         End If
  170.     Next ctlnum%
  171.     Form1.Enabled = True
  172. End Sub
  173. ' Reset the counter, and begin the count
  174. Sub Command6_Click ()
  175.     Dim di%
  176.     di% = PerformCount(0)
  177.     timer1.Enabled = True
  178. End Sub
  179. ' Stop the counter
  180. Sub Command7_Click ()
  181.     timer1.Enabled = False
  182. End Sub
  183. ' This is a function designed to be reentrant without being
  184. ' recursive.
  185. ' mode is 0 to initialize the counter
  186. ' mode is 1 to continue counting
  187. ' Return value is 0 if counting is finished
  188. ' Return value is 1 to continue counting
  189. Function PerformCount% (mode As Integer)
  190.     Static counter&
  191.     Select Case mode
  192.         Case 0
  193.                 counter& = 0
  194.         Case 1
  195.                 counter& = counter& + 1
  196.     End Select
  197.     If counter& = LOOPCOUNT Then
  198.         ToggleColor
  199.         PerformCount% = 0
  200.     Else
  201.         PerformCount% = 1
  202.     End If
  203.     label1.Caption = Str$(counter&)
  204. End Function
  205. Sub Timer1_Timer ()
  206.     Dim res%
  207.     res% = PerformCount(1)
  208.     ' Once the termination condition is reached, shut off
  209.     ' the timer
  210.     If res% = 0 Then timer1.Enabled = False
  211. End Sub
  212. ' This function toggles the background color of the label
  213. ' to make it easier to see when the count ends
  214. Sub ToggleColor ()
  215.     If label1.BackColor = QBColor(15) Then
  216.         label1.BackColor = QBColor(11)
  217.     Else
  218.         label1.BackColor = QBColor(15)
  219.     End If
  220. End Sub
  221.